import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Layout from '@/page/user/layout/layout.vue'
import AdminLayout from '@/page/administrator/layout/layout.vue'

const routes: Array<RouteRecordRaw> = [{
    path: '/',
    redirect: '/user/home',
    children: []
}, {
    path: '/user',
    redirect: '/user/home',
    children: [{
        path: 'home',
        component: Layout,
        children: [{
            path: '',
            name: 'home',
            meta: {
                keep: true,
                keepAlive: true
            },
            component: () => import('@/page/user/home/home.vue')
        }]
    }, {
        path: 'login/userId',
        name: 'userIdLogin',
        component: () => import('@/page/user/login-by-id/login-by-id.vue')
    }, {
        path: 'login/email',
        name: 'emailLogin',
        component: () => import('@/page/user/email-login/email-login.vue')
    }, {
        path: 'register',
        name: 'register',
        component: () => import('@/page/user/register/register.vue')
    }, {
        path: 'user-info',
        component: Layout,
        children: [{
            path: '',
            name: 'userInfo',
            component: () => import('@/page/user/user-info/user-info.vue')
        }]
    }, {
        path: 'setting',
        component: Layout,
        children: [{
            path: '',
            name: 'setting',
            component: () => import('@/page/user/setting/setting.vue')
        }]
    }, {
        path: 'my-info',
        // component: Layout,
        children: [{
            path: '',
            name: 'myInfo',
            component: () => import('@/page/user/my-info/my-info.vue')
        }]
    }, {
        path: 'find',
        component: Layout,
        children: [{
            path: '',
            name: 'find',
            component: () => import('@/page/user/find/find.vue')
        }]
    }, {
        path: 'created',
        component: Layout,
        children: [{
            path: '',
            name: 'created',
            component: () => import('@/page/user/created/created.vue')
        }]
    }, {
        path: 'about',
        name: 'about',
        component: () => import('@/page/user/setting/page/about.vue')
    }, {
        path: 'more-detail/:id/:type',
        name: 'moreDetail',
        component: () => import('@/page/user/more-detail/more-detail.vue')
    }, {
        path: 'poetry-detail/:id',
        name: 'poetryDetail',
        component: () => import('@/page/user/poetry-detail/poetry-detail.vue')
    }, {
        path: 'poems-detail/:poemsId',
        name: 'poemsDetail',
        component: () => import('@/page/user/poems-detail/poems-detail.vue')
    }, {
        path: 'manage-poems/:poemsId',
        name: 'userManagePoems',
        component: () => import('@/page/user/poems-detail/page/manage-poems.vue')
    }, {
        path: 'search',
        name: 'search',
        component: () => import('@/page/user/search/search.vue')
    }, {
        path: 'search-more/:searchType/:searchKey',
        name: 'searchMore',
        component: () => import('@/page/user/search/page/search-more.vue')
    }, {
        path: 'author-detail/:authorId',
        name: 'authorDetail',
        component: () => import('@/page/user/find/page/author-detail.vue')
    }, {
        path: 'user-forgot-password',
        name: 'userForgotPassword',
        component: () => import('@/page/user/login-by-id/page/forgot-password.vue')
    }, {
        path: 'user-forgot-password-reset/:userEmail',
        name: 'userForgotPasswordReset',
        component: () => import('@/page/user/login-by-id/page/forgot-password-reset.vue')
    }]
}, {
    path: '/administrator',
    redirect: '/administrator/login',
    children: [{
        path: 'login',
        name: 'administratorLogin',
        component: () => import('@/page/administrator/login/login.vue')
    }, {
        path: 'forgot-password',
        name: 'forgotPassword',
        component: () => import('@/page/administrator/forgot-password/forgot-password.vue')
    }, {
        path: 'forgot-password-reset/:email',
        name: 'forgotPasswordReset',
        component: () => import('@/page/administrator/forgot-password/page/forgot-password-reset.vue')
    }, {
        path: 'index',
        redirect: '/administrator/index/manage-user',
        component: AdminLayout,
        children: [{
            path: 'home',
            name: 'indexHome',
            component: () => import('@/page/administrator/index/page/home/home.vue'),
        }, {
            path: 'manage-user',
            name: 'manageUser',
            component: () => import('@/page/administrator/index/page/manage-user/manage-user.vue')
        }, {
            path: 'manage-banner',
            name: 'manageBanner',
            component: () => import('@/page/administrator/index/page/manage-banner/manage-banner.vue')
        }, {
            path: 'manage-author',
            name: 'manageAuthor',
            component: () => import('@/page/administrator/index/page/manage-author/manage-author.vue')
        }, {
            path: 'manage-dynasty',
            name: 'manageDynasty',
            component: () => import('@/page/administrator/index/page/manage-dynasty/manage-dynasty.vue')
        }, {
            path: 'manage-poemsList',
            name: 'managePoemsList',
            component: () => import('@/page/administrator/index/page/manage-poemsList/manage-poemsList.vue')
        }, {
            path: 'manage-poemsList-detail/:plId',
            name: 'managePoemsListDetail',
            component: () => import('@/page/administrator/index/page/manage-poemsList/page/poems-list-detail.vue')
        }, {
            path: 'manage-poetry',
            name: 'managePoetry',
            component: () => import('@/page/administrator/index/page/manage-poetry/manage-poetry.vue')
        }, {
            path: 'manage-poetry/:pid',
            name: 'managePoetryDetail',
            component: () => import('@/page/administrator/index/page/manage-poetry/page/manage-poetry-detail.vue')
        }, {
            path: 'manage-create',
            name: 'manageCreate',
            component: () => import('@/page/administrator/index/page/manage-create/manage-create.vue')
        },{
            path: 'manage-notice',
            name: 'manageNotice',
            component: () => import('@/page/administrator/index/page/manage-notice/manage-notice.vue')
        },{
            path: 'manage-type',
            name: 'manageType',
            component: () => import('@/page/administrator/index/page/manage-type-label/manage-type-label.vue')
        },{
            path: 'manage-label',
            name: 'manageLabel',
            component: () => import('@/page/administrator/index/page/manage-label/manage-label.vue')
        }]
    }]
}]

const router = createRouter({
    history: createWebHistory(),
    routes,
})

// let googleServer: GoogleTagServer = new GoogleTagServer()
const whiteList = [
    { pattern: /^\/administrator\/login\/*/ },
    { pattern: /^\/administrator\/forgot-password\/*/ },
    { pattern: /^\/administrator\/forgot-password-reset\/*/ },
    { pattern: /^\/user\/home\/*/ },
    { pattern: /^\/user\/find\/*/ },
    { pattern: /^\/user\/created\/*/ },
    { pattern: /^\/user\/my-info\/*/ },
    { pattern: /^\/user\/login\/*/ },
    { pattern: /^\/user\/setting\/*/ },
    { pattern: /^\/user\/register\/*/ },
    { pattern: /^\/user\/user-forgot-password\/*/ },
    { pattern: /^\/user\/user-forgot-password-reset\/*/ },
]
router.beforeEach((to, from, next) => {
    if (to.name && from.name && to.name === from.name) {
        next(false)
        return false
    } else {
        const token = window.sessionStorage.getItem('token') || ''
        if(!token) {
            let pass = whiteList.some((item: any) => {
                if (typeof (item) === 'object' && item.pattern) {
                    return item.pattern.test(to.fullPath)
                }
                return to.fullPath === item
            })
            if(to.fullPath && !pass) {
                let regx = /administrator/g
                if(!regx.test(to.fullPath)) router.push({ name: 'userIdLogin' })
                else router.push({ name: 'administratorLogin' })
            } else {
                next()
            }
        } else {
            next()
        }
    }
})
export default router
